package com.anke.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.anke.constant.MessageConstant;
import com.anke.entity.Result;
import com.anke.entity.Staff;
import com.anke.service.StaffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/staff")
@CrossOrigin
public class StaffController {
    @Reference
    private StaffService staffService;

    @PostMapping("/add")
    public Result add(@RequestBody Staff staff){
        try{
            System.out.println(staff);
            staffService.StaffAdd(staff);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.REGISTER_FAIL);
        }
        return new Result(true,MessageConstant.REGISTER_SUCCESS);
    }
    @PostMapping("/delete")
    public Result delete(int id){
        try {
            staffService.delete(id);
        }catch(Exception e){
            return new Result(false,MessageConstant.DELETE_FAIL);

        }
        return new Result(true,MessageConstant.DELETE_SUCCESS);
    }
    @PostMapping("/select")
    public List<Staff> SelectAll(){
        List<Staff>list=staffService.StaffAll();
        return list;
    }

    @PostMapping("/selectBy")//按公司查找
    public List<Staff> SelectByCompany(@RequestBody String companyName){
        System.out.println(companyName);
        List<Staff> list = staffService.SelectByCompany(companyName);
        System.out.println(list);
        return list;
    }

    @PostMapping("/hello")
    public List<Staff> PageHelp() {
        PageHelper.startPage(1,2);
        List<Staff> list = staffService.StaffAll();
        PageInfo<Staff> info = new PageInfo(list);
        int page=info.getPages();
        System.out.println(page);
        return list;
    }
}
